<!DOCTYPE html>
<!--[if lte IE 6]><html class="ie6"><!--[if gt IE 8]><!--><html><!--<![endif]-->
<head>
    <title>Rangy Core Demo</title>
    <link href="demo.css" rel="stylesheet" type="text/css">
    <script type="text/javascript" src="../lib/rangy-core.js"></script>

    <script type="text/javascript">
        function gEBI(id) {
            return document.getElementById(id);
        }

        function getFirstRange() {
            var sel = rangy.getSelection();
            return sel.rangeCount ? sel.getRangeAt(0) : null;
        }

        function reportSelectionText() {
            alert( rangy.getSelection().toString() );
        }

        function reportSelectionHtml() {
            alert( rangy.getSelection().toHtml() );
        }

        function inspectSelection() {
            alert( rangy.getSelection().inspect() );
        }

        function deleteSelection() {
            rangy.getSelection().deleteFromDocument();
        }

        function collapseSelectionToStart() {
            rangy.getSelection().collapseToStart();
        }

        function collapseSelectionToEnd() {
            rangy.getSelection().collapseToEnd();
        }

        function showContent(frag) {
            var displayEl = gEBI("selectioncontent");
            var codeEl = gEBI("code");
            while (displayEl.firstChild) {
                displayEl.removeChild(displayEl.firstChild);
            }
            if (frag) {
                displayEl.appendChild(frag);
            }
            codeEl.value = displayEl.innerHTML;
        }

        function inspectRange() {
            var range = getFirstRange();
            if (range) {
                alert(range.inspect());
            }
        }

        function reportRangeHtml() {
            var range = getFirstRange();
            if (range) {
                alert(range.toHtml());
            }
        }

        function extractRange() {
            var range = getFirstRange();
            if (range) {
                showContent(range.extractContents());
            }
        }

        function cloneRange() {
            var range = getFirstRange();
            if (range) {
                showContent(range.cloneContents());
            }
        }

        function deleteRange() {
            var range = getFirstRange();
            if (range) {
                range.deleteContents();
            }
        }

        function surroundRange() {
            var range = getFirstRange();
            if (range) {
                var el = document.createElement("span");
                el.style.backgroundColor = "pink";
                if (range.canSurroundContents(el)) {
                    range.surroundContents(el);
                } else {
                    alert("Unable to surround range because range partially selects a non-text node. See DOM4 spec for more information.");
                }
            }
        }

        function insertNodeAtRange() {
            var range = getFirstRange();
            if (range) {
                var el = document.createElement("span");
                el.style.backgroundColor = "lightblue";
                el.style.color = "red";
                el.style.fontWeight = "bold";
                el.appendChild(document.createTextNode("**INSERTED NODE**"));
                range.insertNode(el);
                rangy.getSelection().setSingleRange(range);
            }
        }

        function createButton(parentNode, clickHandler, value) {
            var button = document.createElement("input");
            button.type = "button";
            button.unselectable = true;
            button.className = "unselectable";
            button.ontouchstart = button.onmousedown = function() {
                clickHandler();
                return false;
            };
            button.value = value;
            parentNode.appendChild(button);
            button = null;
        }

        window.onload = function() {
            rangy.init();

            // Enable multiple selections in IE
            try {
                document.execCommand("MultipleSelection", true, true);
            } catch (ex) {}

            // Create selection buttons
            var selectionButtonsContainer = gEBI("selectionButtons");
            createButton(selectionButtonsContainer, reportSelectionText, "Get selection text");
            createButton(selectionButtonsContainer, inspectSelection, "Inspect selection");
            createButton(selectionButtonsContainer, reportSelectionHtml, "Get selection HTML");
            createButton(selectionButtonsContainer, deleteSelection, "Delete selection");
            createButton(selectionButtonsContainer, collapseSelectionToStart, "Collapse to start");
            createButton(selectionButtonsContainer, collapseSelectionToEnd, "Collapse to end");

            // Create Range buttons
            var rangeButtonsContainer = gEBI("rangeButtons");
            createButton(rangeButtonsContainer, inspectRange, "Inspect");
            createButton(rangeButtonsContainer, reportRangeHtml, "Get HTML");
            createButton(rangeButtonsContainer, extractRange, "Extract");
            createButton(rangeButtonsContainer, cloneRange, "Clone");
            createButton(rangeButtonsContainer, deleteRange, "Delete");
            createButton(rangeButtonsContainer, surroundRange, "Surround (where possible)");
            createButton(rangeButtonsContainer, insertNodeAtRange, "Insert node");

            // Display the control range element in IE
            if (rangy.features.implementsControlRange) {
                gEBI("controlRange").style.display = "block";
            }
        };
    </script>
</head>
<body>
    <div id="buttons">
        <h3>Selection methods</h3>
        <p>The following buttons act on the whole selection:</p>
        <div id="selectionButtons"></div>

        <h3>Range methods</h3>
        <p>The following buttons act on the first range in the current selection:</p>
        <div id="rangeButtons"></div>

        <h4>Selection/Range HTML</h4>
        <textarea rows="10" cols="10" id="code"></textarea>

        <h4>Selection/Range content</h4>
        <div id="selectioncontent"></div>

    </div>
    <div id="content">
    <h1>Rangy Core Demo</h1>

    <p id="intro">
        Please use your mouse and/or keyboard to make selections from the sample content below and use the buttons on
        the left hand size to manipulate the selection.
    </p>

    <p>
        <b>Association football</b> is a sport played between two teams. It is usually called <b>football</b>, but in
        some countries, such as the United States, it is called <b>soccer</b>. In
        <a href="http://simple.wikipedia.org/wiki/Japan">Japan</a>, New Zealand, South Africa, Australia, Canada and
        Republic of Ireland, both words are commonly used.
    </p>
    <p>
        Each team has 11 players on the field. One of these players is the <i>goalkeeper</i>, and the other ten are
        known as <i>"outfield players."</i> The game is played by <b>kicking a ball into the opponent's goal</b>. A
        match has 90 minutes of play, with a break of 15 minutes in the middle. The break in the middle is called
        half-time.
    </p>
    <h2>Competitions <span class="smaller">(this section is editable)</span></h2>
    <p contenteditable="true">
        There are many competitions for football, for both football clubs and countries. Football clubs usually play
        other teams in their own country, with a few exceptions. <b>Cardiff City F.C.</b> from Wales for example, play
        in the English leagues and in the English FA Cup.
    </p>
    <h2>Who plays football <span class="smaller">(this section is editable and in pre-formatted text)</span></h2>
    <pre contenteditable="true">
Football is the world's most popular sport. It is played in more
countries than any other game. In fact, FIFA (the Federation
Internationale de Football Association) has more members than the
United Nations.

It is played by both males and females.


</pre>
    <p id="controlRange" style="display: none" contenteditable="true">
        Some controls to put in a ControlRange in IE: <input> and <input> and <input>
    </p>

    <p class="small">
        Text adapted from <a href="http://simple.wikipedia.org/wiki/Association_football">Simple Wikipedia page on
        Association Football</a>, licensed under the
        <a href="http://simple.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License">Creative
            Commons Attribution/Share-Alike License</a>.
    </p></div>
</body>
</html>